import {
    createRouter,
    createWebHistory
} from "vue-router";
import Layout from "../layout/index.vue";

const files = import.meta.globEager("@/views/**/plugin/**.routes.js")
let AllPics = Object.values(files).map((v) => v.default);
const addRoutes = []
AllPics.forEach(e => {
    if (window.top !== window) { // 如果不是内嵌在别的项目下面，就用自己框架的layout
        e.forEach(ele => {
            ele.meta.type = 'frameOut'
        })
    }
    e && addRoutes.push(...e)
})
/**
 * 在主框架内显示
 */
const frameIn = [
    {
        path: '/',
        component: Layout,
        name: "container",
        redirect: "home",
        meta: {
            requiresAuth: true, //有一些页面是否登录才能进去
            name: "首页",
        },
        children: [
            {
                path: 'myCenter',
                component:() => import('@/views/mycenter/index.vue'),
                name:'myCenter',
                meta: {
                    requiresAuth: true,
                    name: "个人中心",
                }
            },
            {
                path: 'employee',
                component: () => import('@/views/system/employee/index.vue'), // 确保这个组件已经被定义并导入
                name: 'employee',
                meta: {
                    requiresAuth: true,
                    name: "员工管理",
                },
            },
            {
                path:'user',
                component:() => import('@/views/system/user/index.vue'),
                name:'user',
                meta: {
                    requiresAuth: true,
                    name: "用户管理",
                }
            },
            {
                path: 'store',
                component: () => import('@/views/goods/store/index.vue'), // 确保这个组件已经被定义并导入
                name: 'store',
                meta: {
                    requiresAuth: true,
                    name: "店铺管理",
                }
            },
            {
                path: 'order',
                component: () => import('@/views/goods/order/index.vue'), // 确保这个组件已经被定义并导入
                name: 'goodsOrder',
                meta: {
                    requiresAuth: true,
                    name: "订单管理",
                }
            },
            {
                path: 'users',
                component: () => import('@/views/system/user/index.vue'), // 确保这个组件已经被定义并导入
                name: 'users',
                meta: {
                    requiresAuth: true,
                    name: "用户管理",
                }
            },
            {
                path: 'menu',
                component: () => import('@/views/system/menu/index.vue'), // 确保这个组件已经被定义并导入
                name: 'menu',
                meta: {
                    requiresAuth: false,
                    name: "菜单管理",
                }
            },
            {
                path: 'comment',
                component: () => import('@/views/goods/comment/index.vue'), // 确保这个组件已经被定义并导入
                name: 'comment',
                meta: {
                    requiresAuth: false,
                    name: "评论管理",
                }
            },
            ...addRoutes.filter(e => e.meta?.type !== 'frameOut')
        ],
    },

]


/**
 * 在主框架之外显示
 */
const frameOut = [
    ...addRoutes.filter(e => e.meta?.type === 'frameOut')
]

let routes = [
    ...frameIn,
    ...frameOut
]

const router = createRouter({
    history: createWebHistory(),
    routes
});

export default router;